package cn.hy.dataSync.statistics.container.communicator;


import cn.hy.dataSync.config.JobConfig;
import cn.hy.dataSync.config.TaskConfig;
import cn.hy.dataSync.job.State;
import cn.hy.dataSync.statistics.communication.Communication;
import cn.hy.dataSync.statistics.communication.CommunicationTool;
import cn.hy.dataSync.statistics.container.collector.ProcessInnerCollector;
import cn.hy.dataSync.statistics.container.report.ProcessInnerReporter;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * job 容器
 */
@Slf4j
public class JobContainerCommunicator extends AbstractContainerCommunicator {

    public JobContainerCommunicator(JobConfig jobConfig) {
        super(jobConfig);
        super.setCollector(new ProcessInnerCollector(super.getJobId()));
        super.setReporter(new ProcessInnerReporter(this));
    }

    @Override
    public void registerCommunication(final List<TaskConfig> taskConfigs) {
        super.getCollector().registerTaskCommunication(taskConfigs);
    }

    @Override
    public Communication collect() {
        return super.getCollector().collectFromTask();
    }

    @Override
    public State collectState() {
        return this.collect().getState();
    }

    @Override
    public void report(Communication communication) {
        super.getReporter().reportJobCommunication(super.getJobId(), communication);
        // 汇报job的 状态
        log.info(CommunicationTool.Stringify.getSnapshot(communication));
        // 汇报vm 的状态
        reportVmInfo();
    }

    @Override
    public Communication getCommunication(Integer taskId) {
        return super.getCollector().getTaskCommunication(taskId);
    }

    @Override
    public Map<Integer, Communication> getCommunicationMap() {
        return super.getCollector().getTaskCommunicationMap();
    }
}
